import {ReplaySubject} from 'rxjs';
import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaders} from '@angular/common/http';
import {BaseUrl} from './common/api';
import { HttpResponse } from 'selenium-webdriver/http';

@Injectable()

export class TreeService{
    private current: ReplaySubject<any> = new ReplaySubject<any>( 1 );
    private currentTitle: ReplaySubject<any> = new ReplaySubject<any>( 1 )
    private editdialog:ReplaySubject<any> = new ReplaySubject<any>( 1 )
    

    private copyurl=BaseUrl.savewidgeturl;
    private  url1=BaseUrl.listurl;
    private deleteurl=BaseUrl.deleteurl;
    private getdatamenuurl=BaseUrl.getdataMenuUrl;
    private getDataSeturl=BaseUrl.getdataseturl      //数据集维度列url
    
    private headers = {
        headers: new HttpHeaders({
          'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
        })
      };
    // ({
    //     "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    //  });

    constructor(
        private http:HttpClient
    ){
        
    }

    //modal-dialog保存数据
    public setdialogdata(data:any){
        this.editdialog.next(data)
        console.log(data)
    }
    public getdialogdata(){
        console.log(this.editdialog)
        return this.editdialog
    }


    public setdataName(data:any){
        this.currentTitle.next(data)
        console.log(data)
    }
    getdataName():Promise<any>{
        return new Promise((resolve,reject)=>{
            this.currentTitle.subscribe(resolve,reject)
        })
    }

    public setData(data:any){
        this.current.next(data)
    }

    //获取树形目录
    getData():Promise<any>{
        return new Promise((resolve,reject)=>{
            this.http.get(this.url1).subscribe(resolve, reject);
        })
    }

    //数据集接口
    getDataSet(data):Promise<any>{
        if(this.current){
            return new Promise((resolve,reject)=>{
                let param={id:data}
                this.http.post(this.getDataSeturl,null,{params:param}).subscribe(resolve,reject);
            })
        }else{
            alert('没有id')
        }
    }


    public fetch ={
        post: (param,thenFn?): void => {
          var headers = new HttpHeaders();
          headers.append('Content-Type', 'application/x-www-form-urlencoded');
          var formDate = new FormData();
          for (let n in param) {
            formDate.append(n, param[n]);
          }
         this.http.post(this.copyurl,param, {headers:headers}).subscribe(resp => {
            // if (successFn) {
            //   try {
            //     let respBody = JSON.parse(resp);
            //     successFn(respBody);
            //   } catch (e) {
            //     successFn(resp);
            //   }
            // }
            
            // if (thenFn) {
            //   try {
            //     let respBody = JSON.parse(resp);
            //     thenFn(respBody);
            //   } catch (e) {
            //     thenFn(resp);
            //   }
            // }
          });
        }, get: (url: String, param, successFn, thenFn?): void => {
          this.http.get(this.copyurl, {responseType: 'text', params: param}).subscribe(resp => {
            if (successFn) {
              try {
                let respBody = JSON.parse(resp);
                successFn(respBody);
              } catch (e) {
                successFn(resp);
              }
            }
            if (thenFn) {
              try {
                let respBody = JSON.parse(resp);
                thenFn(respBody);
              } catch (e) {
                thenFn(resp);
              }
            }
          });
        }
      };
    //复制接口
    copyNode(data):Promise<any>{
        return new Promise((resolve,reject)=>{
            let parama={id:data}
        this.http.get(this.copyurl,{params:data}).subscribe(resolve,reject)
        })
    }

    //删除接口
    deleteNode(id):Promise<any>{
        return new Promise((resolve,reject)=>{
            let parama={id:id}
            this.http.post(this.deleteurl,null,{params:parama}).subscribe(resolve,reject)
        })
    }

    //获取数据集接口
    getdataMenu(id){
        return new Promise((resolve,reject)=>{
            this.http.get(this.getdatamenuurl).subscribe(resolve,reject)
        })
    }
}